package de.koller.singularity.client;

import de.koller.aframe.util.misc.BufferedRandomFloat;
import de.koller.aframe.util.noise.n2D.ScrollingPerlin2D;
import de.koller.oceanFramework.gl.util.ImagePreview;
import de.koller.oceanFramework.gl.util.RasterManager;
import de.koller.oceanFramework.tree.Child;
import de.koller.oceanFramework.util.ColorIntUtil;
import de.koller.singularity.SNode;
import de.koller.singularity.SRoot;
import de.koller.singularity.client.display.SDisplay;
import de.koller.singularity.client.input.SInput;
import de.koller.singularity.engine.tile.STile;
import de.koller.singularity.engine.tile.TileMapSave;
import java.awt.event.KeyEvent;

public class SClient extends SNode {
	
	@Child public final SDisplay display = new SDisplay();
	@Child public final SInput input = new SInput();
	
	public void click( int x, int y ) {
		long[] t = display.tileDisplay.screenToWorld( x, y );
		sroot.engine.tilemap.setTileAt(
				"default",
				t[0], t[1],
				sroot.engine.tilemap.tileCreator.getTile( "grass" ) );
	}
	
	public void keyPress( int key ) {
		if( key == KeyEvent.VK_UP )    display.tileDisplay.incCamera(  0, -1 );
		if( key == KeyEvent.VK_DOWN )  display.tileDisplay.incCamera(  0,  1 );
		if( key == KeyEvent.VK_LEFT )  display.tileDisplay.incCamera( -1, 0 );
		if( key == KeyEvent.VK_RIGHT ) display.tileDisplay.incCamera(  1, 0 );
		if( key == KeyEvent.VK_G ) generateLand();
		if( key == KeyEvent.VK_P ) testPrintLand();
	}
	
	private void testPrintLand() {
		RasterManager m = new RasterManager( 1024, 1024 );
		for( int x = 0; x < m.getWidth(); x++ )
			for( int y = 0; y < m.getHeight(); y++ ) {
				int r = 0, g = 0, b = 0;
				STile[] ti = sroot.engine.tilemap.getTileAt( "default", x, y );
				for( STile t : ti ) {
					r += ColorIntUtil.getRValue( t.getColor() );
					g += ColorIntUtil.getGValue( t.getColor() );
					b += ColorIntUtil.getBValue( t.getColor() );
				}
				r /= ti.length;
				g /= ti.length;
				b /= ti.length;
				m.setPixel( ColorIntUtil.getIntColor( 0xff, r, g, b ), x, y );
			}
		ImagePreview.showRaster( m, 1024, 1024 );
	}
	
	private void generateLand() {
		BufferedRandomFloat bfr = new BufferedRandomFloat( "./temp/sing", SRoot.RND );
		ScrollingPerlin2D perlin = new ScrollingPerlin2D( bfr, 8, .7f, true );
		
		for( int ix = 0; ix < 2; ix++ ) {
			for( int iy = 0; iy < 2; iy++ ) {
				for( int sx = 0; sx < TileMapSave.CHUNK_SIZE; sx++ ) {
					for( int sy = 0; sy < TileMapSave.CHUNK_SIZE; sy++ ) {
						int x = TileMapSave.CHUNK_SIZE * ix + sx;
						int y = TileMapSave.CHUNK_SIZE * iy + sy;
						
						float f = perlin.getValue( 1f / (TileMapSave.CHUNK_SIZE*2) * x * 5,
												   1f / (TileMapSave.CHUNK_SIZE*2) * y * 5);
						String tile;
						if( f > .5f )
							tile = "grass";
						else
							tile = "deepSea";
						
						sroot.engine.tilemap.setTileAt( "default", x, y, 
								sroot.engine.tilemap.tileCreator.getTile( tile ) );
					}
				}
				System.out.println( ix + " / " + iy );
			}
		}
		
	}
	
}
